<template>
  <div>
    <el-row>
      <el-col>
        <el-form :inline="true">
          <el-form-item>
            <el-input style="width: 500px;" v-model="search" placeholder="关键词" clearable @clear="getAllBooks"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="addBooks">添加</el-button>
            <el-button type="info" @click="searchBooks">查询</el-button>
          </el-form-item>
        </el-form>

      </el-col>
    </el-row>
    <el-row>
      <el-table
          fixed
          :data="tableData"
          style="width: 100%">
        <el-table-column
            fixed
            type="index"
            label="序号"
            width="60">
        </el-table-column>
        <el-table-column
            show-overflow-tooltip
            prop="booksName"
            label="书名"
            width="160">
        </el-table-column>
        <el-table-column
            show-overflow-tooltip
            prop="booksDesc"
            label="简介"
            width="400">
        </el-table-column>

        <el-table-column
            prop="booksIsbn"
            label="ISBN号"
            width="140">
        </el-table-column>

        <el-table-column
            show-overflow-tooltip
            prop="booksImg"
            label="图书封面"
            width="230">
        </el-table-column>

        <el-table-column
            prop="booksNum"
            label="库存"
            width="100">
        </el-table-column>

        <el-table-column
            prop="booksPrice"
            label="售价(元)"
            width="120">
        </el-table-column>

        <el-table-column
            prop="booksCategoryId"
            label="类别"
            width="150">
        </el-table-column>

        <el-table-column
            prop="booksState"
            label="状态"
            width="150">
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="edit(scope.$index)">编辑</el-button>
            <el-button
                size="mini"
                type="danger"
                @click="del(scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>

    <el-dialog title="图书信息" :visible.sync="bookFormVisible">
      <el-form :model="form">
        <el-form-item label="书名" :label-width="formLabelWidth">
          <el-input v-model="form.booksName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="简介" :label-width="formLabelWidth">
          <el-input type="textarea" v-model="form.booksDesc" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="ISBN号" :label-width="formLabelWidth">
          <el-input v-model="form.booksIsbn" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="封面(URL)" :label-width="formLabelWidth">
          <el-input v-model="form.booksImg" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="库存(本)" :label-width="formLabelWidth">
          <el-input v-model="form.booksNum" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="售价(元)" :label-width="formLabelWidth">
          <el-input v-model="form.booksPrice" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="类别" :label-width="formLabelWidth">
          <el-input v-model="form.booksCategoryId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-input v-model="form.booksState" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="bookFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="checkEdit">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="图书信息" :visible.sync="addFormVisible">
      <el-form :model="addform">
        <el-form-item label="书名" :label-width="formLabelWidth">
          <el-input v-model="addform.booksName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="简介" :label-width="formLabelWidth">
          <el-input type="textarea" v-model="addform.booksDesc" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="ISBN号" :label-width="formLabelWidth">
          <el-input v-model="addform.booksIsbn" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="封面(URL)" :label-width="formLabelWidth">
          <el-input v-model="addform.booksImg" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="库存(本)" :label-width="formLabelWidth">
          <el-input v-model="addform.booksNum" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="售价(元)" :label-width="formLabelWidth">
          <el-input v-model="addform.booksPrice" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="类别" :label-width="formLabelWidth">
          <el-input v-model="addform.booksCategoryId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-input v-model="addform.booksState" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="bookFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">添加</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Book",
  data() {
    return {
      tableData: [],
      bookFormVisible:false,
      addFormVisible:false,
      formLabelWidth:'120px',
      form: {
        booksName: "",
        booksDesc:"",
        booksIsbn:"",
        booksImg:"",
        booksNum:"",
        booksPrice:"",
        booksCategoryId:"",
        booksState:"",

      },
      addform: {
        booksName: "",
        booksDesc:"",
        booksIsbn:"",
        booksImg:"",
        booksNum:"",
        booksPrice:"",
        booksCategoryId:"",
        booksState:"",

      }
    }
  },
  methods:{
    getAllBooks() {
      this.$axios.get("/goods/getall").then(res => {
        if(res.data.code == 0) {
          this.tableData = res.data.data
          this.$message.success("获取成功")
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    edit(index) {
      this.form = this.tableData[index]
      this.bookFormVisible = true
    },
    checkEdit() {
      this.$axios.post("/goods/update",this.form).then(res => {
        if(res.data.code == 0) {
          this.$message.success("修改成功")
          location.reload()
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    del(index) {
      this.$confirm('确认删除该商品吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.post("/goods/delete",this.tableData[index]).then(res => {
          if(res.data.code == 0) {
            this.$message.success("删除成功")
          } else {
            this.$message.error(res.data.message)
          }
        })
      }).catch();
      location.reload()
    },
    addBooks() {
      this.addFormVisible = true
    },
    add() {
      this.$axios.post("/goods/add",this.addform).then(res => {
        if(res.data.code == 0) {
          this.addFormVisible = false
          this.addform =  {
            booksName: "",
            booksDesc:"",
            booksIsbn:"",
            booksImg:"",
            booksNum:"",
            booksPrice:"",
            booksCategoryId:"",
            booksState:"",

          }
          location.reload()
          this.$message.success("添加成功")
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    searchBooks(){}
  },
  created() {
    this.getAllBooks()
  }
}
</script>

<style scoped>

</style>